package com.edu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.GoodsToWeb;
import com.edu.entity.Tool;
import com.edu.entity.User;
import com.edu.entity.goodsDTO;
import com.edu.entity.reGoodsAndUser;
import com.edu.service.impl.GoodServerImpl;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class searchGoodServlet
 */
@WebServlet(name = "searchGoodsDo", urlPatterns = { "/searchGoodsDo" })
public class searchGoodServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("search2.jsp").forward(request, response);
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		GoodServerImpl gsi = new GoodServerImpl();
		Tool t1 = new Tool();
		ObjectMapper om = new ObjectMapper();
		goodsDTO DTO = null;
		String searchName = request.getParameter("searchName");
		try {
			DTO = om.readValue(request.getReader(), goodsDTO.class);
			Goods g = new Goods();
			Conditions con = new Conditions();
			g.setGoodname(DTO.getSearchName());
			g.setGoodtype(DTO.getScon());
			if(DTO.isCon2()) {
				g.setGoodTradetType(1);
			}
			if(DTO.isCon3()) {
				g.setGoodstat(1);
			}
			con.setOrderType(DTO.getOrderType());
			ArrayList<GoodsToWeb> gtw = gsi.indexFindGoods(g, con);
			ArrayList<reGoodsAndUser> rgaList = new ArrayList();
			for (GoodsToWeb goodsToWeb : gtw) {
				reGoodsAndUser rga = new reGoodsAndUser();
				User u = gsi.findSeller(goodsToWeb.getSellerId());
				rga.setGoodslist(goodsToWeb);
				rga.setUser(u);
				rgaList.add(rga);
			}
			String json = om.writeValueAsString(rgaList);
			PrintWriter pw = response.getWriter();
			pw.print(json);
			pw.flush();
		}catch(JsonMappingException e) {
			System.out.println("获取DTO失败");
			System.out.println("搜索名为："+searchName);
			Goods g = new Goods();
			g.setGoodname(searchName);
			Conditions con = null;
			ArrayList<GoodsToWeb> gtw = gsi.indexFindGoods(g, con);
			ArrayList<reGoodsAndUser> rgaList = new ArrayList();
			for (GoodsToWeb goodsToWeb : gtw) {
				reGoodsAndUser rga = new reGoodsAndUser();
				User u = gsi.findSeller(goodsToWeb.getSellerId());
				rga.setGoodslist(goodsToWeb);
				rga.setUser(u);
				rgaList.add(rga);
			}
			String json = om.writeValueAsString(rgaList);
			System.out.println(json);
			PrintWriter pw = response.getWriter();
			pw.print(json);
			pw.flush();
		}
		
		
	}

}
